package com.fatdetection.demo.service.imp;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fatdetection.demo.bean.Project;
import com.fatdetection.demo.mapper.ProjectMapper;
import com.fatdetection.demo.service.ProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;

@Service
public class ProjectServiceImp implements ProjectService {

    @Autowired
    ProjectMapper projectMapper;

    @Override
    public List<Project> searchProjsByAdminName(String name, String info) {
        return projectMapper.selectList(new QueryWrapper<Project>()
                .eq("admin_name", name)
        .like("project_name", info));
    }

    @Override
    public int insertProject(Project project) {
        return projectMapper.insert(project);
    }

    @Override
    public int delProject(Integer id) {
        return projectMapper.delete(new QueryWrapper<Project>().eq("project_id", id));
    }

    @Override
    public int updateProject(Project project) {
        return projectMapper.update(project, new UpdateWrapper<Project>().eq("project_id", project.getProjectId()));
    }

    @Override
    public List<Project> findAll() {
        return projectMapper.selectList(null);
    }

    @Override
    public Project findById(Integer id) {
        return projectMapper.selectOne(new QueryWrapper<Project>().eq("project_id", id));
    }

    @Override
    public List<Project> findProjByAdmin(String name) {
        return projectMapper.selectList(new QueryWrapper<Project>().eq("admin_name", name));
    }

    @Override
    public Project findProjByName(String projectName) {
        return projectMapper.selectOne(new QueryWrapper<Project>().eq("project_name", projectName));
    }

    @Override
    public Page<Project> selectAllPage(Integer current, Integer size, String searchKey, String searchVal) {
        Page<Project> page = new Page<>(current, size);
        QueryWrapper<Project> query = new QueryWrapper<Project>().like(searchKey, searchVal);
        if(searchKey == null || searchVal == null){
            query = null;
        }
        return projectMapper.selectPage(page, query);
    }

    @Override
    public List<Project> findMeasProj(Integer stateMeas) {
        return projectMapper.selectList(new QueryWrapper<Project>().eq("state_measure", stateMeas));
    }

    @Override
    public List<Project> findDataProj(Integer stateData) {
        return projectMapper.selectList(new QueryWrapper<Project>().eq("state_data", stateData));
    }
}
